# -*- coding: utf-8 -*-
from datetime import timedelta
from utils.operators.spark_submit import SparkSubmitOperator
from contextlib import ExitStack

# 设置上游依赖
jms_dwd__dwd_centersend_timerate_all_detail_dt = SparkSubmitOperator(
    task_id='jms_dwd__dwd_centersend_timerate_all_detail_dt',
    email=['wangmenglei@jtexpress.com', 'yl_bigdata@yl-scm.com'],
    pool_slots=4,
    # depends_on_past=True,  # 如果任务依赖于前一天的同名任务，则将 depends_on_past 设为 True
    task_concurrency=1,  # 如果任务不支持并发，则将 task_concurrency 设为 1
    name='jms_dwd__dwd_centersend_timerate_all_detail_dt_{{ execution_date | date_add(1) | cst_ds }}',  # yarn 任务名称
    driver_memory='28G',
    driver_cores=2,
    executor_memory='30G',
    executor_cores=4,
    num_executors=100,
    conf={'spark.executor.memoryOverhead': 2048,
          'spark.core.connection.ack.wait.timeout': 300,
          'spark.sql.shuffle.partitions': 1000,
          'spark.locality.wait': 60,
          'spark.sql.broadcastTimeout': 3600,
          'spark.sql.autoBroadcastJoinThreshold': 104857600
          },
    # jars='hdfs:///user/oozie/chenhoukun/report/centersend_timerate_data/pro/common-1.0-SNAPSHOT.jar',  # 依赖 jar 包
    jars='hdfs:///scheduler/jms/spark/chk/report/centersend_timerate_data/pro/common-1.0-SNAPSHOT.jar',  # 依赖 jar 包
    java_class='com.yunlu.bigdata.jobs.report.CenterSendTimerateAllDetailData',  # spark 主类
    application='hdfs:///scheduler/jms/spark/chk/report/centersend_timerate_data/pro/original-jobs-1.0-SNAPSHOT.jar',
    # application='hdfs:///user/oozie/chenhoukun/report/centersend_timerate_data/pro/original-jobs-1.0-SNAPSHOT.jar',  # spark jar 包
    # spark jar 包
    application_args=['{{ execution_date  | date_add(1)| cst_ds }}'],  # 参数dt 2020-10-26
    #execution_timeout=timedelta(hours=1)
    #excel平均时长:13分9秒
    execution_timeout = timedelta(minutes=100),
    retries=0
)

def set_deps():
    from jms.ods.tms.yl_tms_vehicle_type import jms_ods__yl_tms_vehicle_type
    from jms.dwd.tab.dwd_barscan_centerarrival_dt import jms_dwd__dwd_barscan_centerarrival_dt
    from jms.ods.tms.yl_tmsnew_branch_shipment_stop import jms_ods__yl_tmsnew_branch_shipment_stop
    from jms.ods.tab.tab_end_piece import jms_ods__tab_end_piece
    from jms.ods.tab.tab_barscan_centerarrival import jms_ods__tab_barscan_centerarrival
    from jms.dwd.tab.dwd_barscan_unloading_dt import jms_dwd__dwd_barscan_unloading_dt
    from jms.ods.tab.tab_barscan_centersend import jms_ods__tab_barscan_centersend
    from jms.ods.lmdm.yl_lmdm_sys_second_code import jms_ods__yl_lmdm_sys_second_code
    from jms.ods.lmdm.yl_lmdm_sys_first_code import jms_ods__yl_lmdm_sys_first_code
    from jms.ods.mysql.sys_manage_region import jms_ods__sys_manage_region
    from jms.ods.tab.tab_barscan_loading import jms_ods__tab_barscan_loading
    from jms.ods.tms.yl_tmsnew_tms_shipment import jms_ods__yl_tmsnew_tms_shipment
    from jms.ods.tms.yl_tmsnew_tms_shipment_stop import jms_ods__yl_tmsnew_tms_shipment_stop
    from jms.ods.tms.yl_tmsnew_tms_vehicle_line import jms_ods__yl_tmsnew_tms_vehicle_line
    from jms.dwd.tab.dwd_tab_reback_transfer_express_base import jms_dwd__dwd_tab_reback_transfer_express_base
    from jms.dim.dim_sys_network_detail_dt import jms_dim__dim_sys_network_detail_dt
    from jms.dm.dm_missing_bind import jms_dm__dm_missing_bind_dt
    from jms.ods.tab.tab_barscan_difficult import jms_ods__tab_barscan_difficult
    from jms.ods.tms.yl_tmsnew_branch_shipment import jms_ods__yl_tmsnew_branch_shipment
    from jms.dwd.oms.dwd_yl_oms_oms_waybill_incre_dt import jms_dwd__dwd_yl_oms_oms_waybill_incre_dt
    from jms.dwd.tab.dwd_tab_report_errorbagging_detail_dt import jms_dwd__dwd_tab_report_errorbagging_detail_dt
    from jms.ods.oms.yl_oms_interceptorpiece import jms_ods__yl_oms_interceptorpiece
    from jms.dws.dws_actual_aging_sign_dt import jms_dws__dws_actual_aging_sign_dt
    from jms.dwd.oms.dwd_oms_order_dt import jms_dwd__dwd_oms_order_dt
    from jms.ods.tab.tab_bulk_cargo_shift import jms_ods__tab_bulk_cargo_shift

# 设置依赖
    jms_dwd__dwd_centersend_timerate_all_detail_dt << [
        jms_ods__yl_tms_vehicle_type,
        jms_dwd__dwd_barscan_centerarrival_dt,
        jms_ods__yl_tmsnew_branch_shipment_stop,
        jms_ods__tab_end_piece,
        jms_ods__tab_barscan_centerarrival,
        jms_dwd__dwd_barscan_unloading_dt,
        jms_ods__tab_barscan_centersend,
        jms_ods__yl_lmdm_sys_second_code,
        jms_ods__yl_lmdm_sys_first_code,
        jms_ods__sys_manage_region,
        jms_ods__tab_barscan_loading,
        jms_ods__yl_tmsnew_tms_shipment,
        jms_ods__yl_tmsnew_tms_shipment_stop,
        jms_ods__yl_tmsnew_tms_vehicle_line,
        jms_dwd__dwd_tab_reback_transfer_express_base,
        jms_dim__dim_sys_network_detail_dt,
        jms_dm__dm_missing_bind_dt,
        jms_ods__tab_barscan_difficult,
        jms_ods__yl_tmsnew_branch_shipment,
        jms_dwd__dwd_yl_oms_oms_waybill_incre_dt,
        jms_dwd__dwd_tab_report_errorbagging_detail_dt,
        jms_ods__yl_oms_interceptorpiece,
        jms_dws__dws_actual_aging_sign_dt,
        jms_dwd__dwd_oms_order_dt,
        jms_ods__tab_bulk_cargo_shift
    ]

# 延迟执行设置上游依赖，避免 import python 包的时候其他 Operator 尚未加载完成引起的循环依赖错误
with ExitStack() as stack:
    stack.callback(set_deps)
